package gameClient.thread;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 用于发送玩家消息（用户名/发牌/出牌/抢地主消息）
 */
public class SendThread extends Thread {
    //指定客户端与服务器端通信的socket
    public Socket socket;
    //存储发送的消息
    public String message;
    //是否正在运行
    public boolean isRun = true;

    public Socket getSocket() {
        return socket;
    }
    public void setSocket(Socket socket) {
        this.socket = socket;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public boolean isRun() {
        return isRun;
    }
    public void setRun(boolean run) {
        isRun = run;
    }

    //构造函数
    public SendThread(){}
    public SendThread(Socket socket) {
        this.socket = socket;
    }
    public SendThread(Socket socket, String message) {
        this.socket = socket;
        this.message = message;
    }

    //负责发消息的具体过程
    @Override
    public void run() {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            while(true){
//                if(isRun == false){
//                    break;
//                }
                //发送消息
                if(message!=null){
                    System.out.println("正在发送消息："+message);
                    dataOutputStream.writeUTF(message);
                    message=null;//避免混淆
                }
                //暂停等待新消息传入
                Thread.sleep(100);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
